package com.bank.user.util;

import com.alibaba.fastjson.support.odps.udf.CodecCheck;
import com.bank.model.entity.*;
import com.bank.model.enums.AccountTypeEnum;
import com.bank.model.enums.StatusEum;
import com.bank.model.enums.UserStatusEnum;
import com.bank.common.stringUtil.UUIDUtils;
import com.bank.model.vo.login.RegisterVo;
import com.bank.model.vo.user.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Cejay1314
 * @version 1.0
 * @create 2023/3/22 15:46
 */
@Component
public class EntityFactory {
    @Autowired
    MapperFactory mapperFactory;
    User user;
    UserRole userRole;
    Map<String,Role> rolesMap;
    UserAccount userAccount;
    Account account;

//    EntityFactory(){
//    }
    @Autowired
    void initRolesMap(){
        rolesMap = new HashMap<String, Role>();
        List<Role> roles = mapperFactory.getRoleMapper().selectList(null);
        for (Role role:roles) {
            rolesMap.put(role.getName(),role);
        }
    }
    public Map<String,Role> getRolesMap(){
        return this.rolesMap;
    }

    void initUser(){
        this.user = new User();
        this.user.setUsername("none");
        this.user.setPassword("123");
        this.user.setPhoneNumber("123");
        this.user.setNickname("steven");
        this.user.setSex("男");
        this.user.setStatus(UserStatusEnum.OK.getCode());
        this.user.setBirthdate(new Date().toString());
        this.user.setIdentityNumber("123456789");
        this.user.setHeadPortrait("boy.png");
        this.user.setSalt(UUIDUtils.getUUID());
        this.user.setAge(20);
        this.user.setCreatedTime(new Date());
        this.user.setUpdatedTime(new Date());
    }
    public User getUser(RegisterVo registerVo){
        initUser();
        this.user.setUsername(registerVo.getUsername());
        this.user.setPassword(registerVo.getPassword());
        this.user.setPhoneNumber(registerVo.getPhoneNumber());
        this.user.setIdentityNumber(registerVo.getIdentityNumber());
        return this.user;
    }
    public User getUser(UserVo userVo){
        initUser();
        this.user.setUsername(userVo.getUsername());
        this.user.setSex(userVo.getSex());
        this.user.setPhoneNumber(userVo.getPhoneNumber());
        this.user.setBirthdate(userVo.getBirthdate());
        this.user.setIdentityNumber(userVo.getIdentityNumber());
        this.user.setAge(userVo.getAge());
        if(userVo.getSex().equals("女")){
            this.user.setHeadPortrait("girl.png");
        }
        return this.user;
    }
    public User getUser(String name, String sex, String phoneNumber, String birthdate, String identityNumber, Integer age){
        initUser();
        this.user.setUsername(name);
        this.user.setPhoneNumber(phoneNumber);
        this.user.setSex(sex);
        this.user.setAge(age);
        this.user.setBirthdate(birthdate);
        this.user.setIdentityNumber(identityNumber);
        if(sex.equals("女")){
            this.user.setHeadPortrait("girl.png");
        }
        return this.user;
    }

    void initUserRole(){
        this.userRole = new UserRole();
        this.userRole.setRoleId(0l);
        this.userRole.setUserId(0l);
        this.userRole.setCreatedTime(new Date());
        this.userRole.setUpdatedTime(new Date());
    }
    public UserRole getUserRole(long roleId,long userId){
        initUserRole();
        this.userRole.setRoleId(roleId);
        this.userRole.setUserId(userId);
        return this.userRole;
    }
    public Role getRole(String roleName){
        return this.rolesMap.get(roleName);
    }

    void initUserAccousnt(){
        this.userAccount = new UserAccount();
    }
    public UserAccount getUserAccount(long userId,String accountId){
        initUserAccousnt();
        this.userAccount.setUserId(userId);
        this.userAccount.setAccountId(accountId);
        return this.userAccount;
    }

    void initAccount(){
        this.account = new Account();
        this.account.setMoney("0");
        this.account.setSalt("000");
        this.account.setStatus(AccountTypeEnum.Deposit.getCode());
    }
    public Account getAccount(String accountId,String password){
        initAccount();
        this.account.setAccountId(accountId);
        this.account.setPassword(password);
        return this.account;
    }


}
